package com.example.administrator.baikangxing.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Sushil_Shen on 2017/4/20/0020.
 * Contact: Sushil_shen@163.com
 */

public class TimeUtils {

    /*分钟转换小时*/
    public static int[] minTohours(String min){
        int total= Integer.valueOf(min);
        int hour = total / 60;
        int minute = total % 60;
        return new int[]{hour,minute};
    }

    /*当前的时间*/
    public static String getNowTime(){
        System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date(System.currentTimeMillis()));
        return format;
    }

    /*前四周的时间*/
    public static String fourWeek(){
        System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date(System.currentTimeMillis()- 2419200000L));
        return format;
    }

    /*2017-05-04T14:34:58转换格式*/
    public static String changeTime(String time){
        String[] ts = time.split("T");
        String[] begin = ts[0].split("-");
        String[] end = ts[1].split(":");
        String s = begin[1]+"月"+begin[2]+"日 "+end[0]+":"+end[1];
        return s;
    }
    //获取当前时间
    public static String getNowDay(){
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String date = sDateFormat.format(curDate);
        return date;
    }
    //获取一周前的时间
    public static String getAgoDay(){
        Date dat = null;
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, -7);
        dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp date = Timestamp.valueOf(dformat.format(dat));
        return String.valueOf(date).substring(0,10);
        /*SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis()-60*60*60*24*7);
        String date = sDateFormat.format(curDate);
        return date;*/
    }
    //获取今天是今年的第几周
    public static String getWeek(){
        Calendar cal = Calendar.getInstance();
        int week = cal.get(cal.WEEK_OF_YEAR);
        return String.valueOf(week);
    }

    //通过某年的几周获取该周的开始时间和结束时间
    public static String getDateFromWeekNo(int year,int weekNo){
        String startDayOfWeekNo = getStartDayOfWeekNo(year, weekNo);
        String endDayOfWeekNo = getEndDayOfWeekNo(year, weekNo);
        return startDayOfWeekNo+"-"+endDayOfWeekNo;
    }


    public static String getStartDayOfWeekNo(int year, int weekNo){
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        return  (cal.get(Calendar.MONTH) + 1) + "." +
                cal.get(Calendar.DAY_OF_MONTH);

    }
    public static String getEndDayOfWeekNo(int year, int weekNo){
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return (cal.get(Calendar.MONTH) + 1) + "." +
                cal.get(Calendar.DAY_OF_MONTH);
    }
    public String getLastDayOfMonth(int year,int month){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR , year);
        calendar.set(Calendar.MONTH , month - 1);
        calendar.set(Calendar.DATE , 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR , -1);
        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" +
                calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * get Calendar of given year
     * @param year
     * @return
     */
    private static Calendar getCalendarFormYear(int year){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        return cal;
    }
}
